<?php

namespace app\controller\admin;

use app\BaseController;
use app\Code;
use app\dao\ProjectDao;
use app\validate\ProjectValidate;
use think\exception\ValidateException;
use think\facade\Request;
use think\facade\Log;

class ProjectController extends BaseController
{
    // 创建竞赛项目
    public function create()
    {
        $data = Request::post();
        $adminId = request()->user['id'] ?? null;
//
//        if (!$adminId) {
//            return $this->returnJson([], Code::UNAUTHORIZED, '未登录或无权限');
//        }

        try {
            validate(ProjectValidate::class)->scene('create')->check($data);

            $project = ProjectDao::createProject(
                $data['name'],
                $data['description'] ?? '',
                $adminId
            );

            return $this->returnJson($project);

        } catch (ValidateException $e) {
            return $this->returnJson([], Code::UNPROCESSABLE_ENTITY, $e->getMessage());

        } catch (\Exception $e) {
            Log::error("创建竞赛项目失败: " . $e->getMessage());
            return $this->returnJson([], Code::SERVER_ERROR, '服务器错误，请稍后再试');
        }
    }
}
